package me.zfz.business.dao.impl;

import com.mixsmart.utils.StringUtils;
import me.zfz.base.dao.impl.BaseDaoImpl;
import me.zfz.base.exception.DaoException;
import me.zfz.base.filter.HandleFilterParam;
import me.zfz.base.res.SQLResUtil;
import me.zfz.base.res.sqlmap.SqlMapping;
import me.zfz.business.bean.UserSearchParam;
import me.zfz.business.bean.entity.StudentInfo;
import me.zfz.business.bean.entity.TNUser;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author author
 */
@Repository("stuDao")
public class StudentInfoDao extends BaseDaoImpl<StudentInfo> {

    private SqlMapping sqlMap;
    private Map<String, Object> params;

    public StudentInfoDao() {
        sqlMap = SQLResUtil.getBusinessSqlMap();
    }

    /**
     * 查询列表
     *
     * @param searchParam 搜索参数
     * @param start
     * @param rows
     * @return
     */
    public List<Object> queryObjPage(UserSearchParam searchParam, int start, int rows) throws DaoException {
        List<Object> lists = null;
        params = new HandleFilterParam(searchParam).getParams();
        String sql = sqlMap.getSQL("stu_list");
        if (StringUtils.isNotEmpty(sql)) {
            lists = queryObjSql(sql, params, start, rows);
        }
        return lists;
    }


    /**
     * 统计用户列表--
     *
     * @param searchParam 搜索参数
     * @return
     */
    public long queryObjCount(UserSearchParam searchParam) throws DaoException {
        long total = 0;
        params = new HandleFilterParam(searchParam).getParams();
        String sql = sqlMap.getSQL("stu_list");
        if (StringUtils.isNotEmpty(sql)) {
            total = countSql(sql, params);
        }
        return total;
    }

    @Override
    public boolean delete(Serializable id) throws DaoException {
        boolean is = false;
        if (null != id && StringUtils.isNotEmpty(id.toString())) {
            String[] ids = id.toString().split(",");
            String sqls = sqlMap.getSQL("del_stu");
            if (StringUtils.isNotEmpty(sqls)) {
                String[] sqlArray = sqls.split(";");
                params = new HashMap<String, Object>(1);
                params.put("stuIds", ids);
                int count = 0;
                for (int i = 0; i < sqlArray.length; i++) {
                    count += executeSql(sqlArray[i], params);
                }
                is = count > 0 ? true : false;
            }
        }
        return is;
    }

    public StudentInfo find(Serializable id) throws DaoException {
        StudentInfo studentInfo = null;
        if (null == id || StringUtils.isEmpty(id.toString())) {
            return studentInfo;
        }
        String sql = sqlMap.getSQL("stu_find_by_id");
        if (StringUtils.isEmpty(sql)) {
            return studentInfo;
        }
        params = new HashMap<>();
        params.put("id", id);
        try {
            List<StudentInfo> studentInfos = querySql(sql, params);
            studentInfo = studentInfos.get(0);
        } catch (Exception e) {
            throw new DaoException(e);
        }
        return studentInfo;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<StudentInfo> findAll() throws DaoException {
        List<StudentInfo> studentInfos = null;
        String sql = sqlMap.getSQL("stu_list");
        if (StringUtils.isEmpty(sql)) {
            return studentInfos;
        }
        try {
            SQLQuery sqlQuery = (SQLQuery) super.getQuery(sql, true);
            sqlQuery.addEntity(TNUser.class);
            studentInfos = sqlQuery.list();
        } catch (Exception e) {
            throw new DaoException(e);
        }
        return studentInfos;
    }

}
